$(function () {
    var selectCompany = $("#productBank");//所属银行
    var selectOrg = $("#productSerie");//产品系列
    $.ajax({
        url: "getproductindexSelect",
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                var company = data.allBankName;//所属银行
                var serie = data.allSerie;//产品系列
                // console.log("company:"+company.length);
                // console.log("serie:"+serie.length);
                selectCompany.html("");
                selectOrg.html("");
                //向所属银行select传入数据
                var options = "<option value=\"0\">---全部---</option>";
                for (var i = 0; i < company.length; i++) {
                    options += "<option value=\"" + company[i].id + "\">" + company[i].bankname + "</option>";
                }
                //向产品系列select传入数据
                var optionss = "<option value=\"0\">---全部---</option>";
                for (var i = 0; i < serie.length; i++) {
                    optionss += "<option value=\"" + serie[i].id + "\">" + serie[i].serieName + "</option>";
                }

                selectCompany.html(options);
                selectOrg.html(optionss);
            }
        }
    })
})

//判断产品名称是否为空
function addproductName() {
    var productName = document.getElementById("productName").value;
    if (productName == "") {
        $("#redProductName").html("名称不能为空！");
        return;
    } else {
        $("#redProductName").html("");
    }
}

//判断额度下线是否为空
function addbenifitsDown() {
    var benifitsDown = document.getElementById("benifitsDown").value;
    if (benifitsDown == "") {
        $("#redBenifitsDown").html("额度下线不能为空！");
        return;
    } else {
        $("#redBenifitsDown").html("");
    }
}

//判断额度上线是否为空
function addbenifitsUp() {
    var benifitsUp = document.getElementById("benifitsUp").value;
    if (benifitsUp == "") {
        $("#redBenifitsUp").html("额度上线不能为空！");
        return;
    } else {
        $("#redBenifitsUp").html("");
    }
}

//判断最短期限是否为空
function addshortestPeriod() {
    var shortestPeriod = document.getElementById("shortestPeriod").value;
    if (shortestPeriod == "") {
        $("#redShortestPeriod").html("最短期限不能为空！");
        return;
    } else {
        $("#redShortestPeriod").html("");
    }
}

//判断最长期限是否为空
function addlongestPeriod() {
    var longestPeriod = document.getElementById("longestPeriod").value;
    if (longestPeriod == "") {
        $("#redLongestPeriod").html("最长期限不能为空！");
        return;
    } else {
        $("#redLongestPeriod").html("");
    }
}

//判断利率下限是否为空
function addrateDown() {
    var rateDown = document.getElementById("rateDown").value;
    if (rateDown == "") {
        $("#redRateDown").html("利率下限不能为空！");
        return;
    } else {
        $("#redRateDown").html("");
    }
}

//判断利率上限是否为空
function addrateUp() {
    var rateUp = document.getElementById("rateUp").value;
    if (rateUp == "") {
        $("#redRateUp").html("利率上限不能为空！");
        return;
    } else {
        $("#redRateUp").html("");
    }
}


$("#myFile").change(function () {
    var logo = document.getElementById('myFile').files[0].name;
    $("#img1").attr("src", "incremental/img/" + logo);
    $("#img1").attr("value", "incremental/img/" + logo);
})

//新建产品
function addProduct() {
    var bankid = $("#productBank").val();
    var loanName = $("#productName").val();
    var seriesid = $("#productSerie").val();
    var benefitsDown = $("#benifitsDown").val();
    var benefitsUp = $("#benifitsUp").val();
    var shortestperiod = $("#shortestPeriod").val();
    var longestperiod = $("#longestPeriod").val();
    var rateDown = $("#rateDown").val();
    var rateUp = $("#rateUp").val();
    var logo = $('#img1').attr('src');

    if (bankid == 0 | loanName == "" | seriesid == 0 | benefitsDown == "" | benefitsUp == "" | shortestperiod == "" | longestperiod == "" | rateDown == "" | rateUp == "" | logo == "") {
        alert("带*的问题不能为空")
        return;
    }
    $.ajax({
        url: "addProduct",
        type: "post",
        async: true,
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        data: {
            bankid: $("#productBank").val(),
            loanName: $("#productName").val(),
            seriesid: $("#productSerie").val(),
            definition: $("#productDefinition").val(),
            customer: $("#productCustomer").val(),
            benefitsDown: $("#benifitsDown").val(),
            benefitsUp: $("#benifitsUp").val(),
            shortestperiod: $("#shortestPeriod").val(),
            longestperiod: $("#longestPeriod").val(),
            rateDown: $("#rateDown").val(),
            rateUp: $("#rateUp").val(),
            logo: $('#img1').attr('src'),
        },
        //请求成功后，回调函数
        success: function (data) {
            if (data.addResult == 1) {
                alert("新建产品成功");
                loadPage('incremental/add_product.html');
            } else {
                alert("新建产品失败");
            }
        },
        error: function (data) {
            alert("新建产品失败");
        }
    })
}
